﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace Pcb.Common
{
    public class _Email
    {       
        /// <summary> 
        /// 发送邮件(只支持.net2.0以上框架) 
        /// <remarks> 例子： Vincent._Email.SendEmail("LLorJJ999@163.com", "773872823@qq.com", "测试", "aadsdfds", "smtp.163.com", "LLorJJ999@163.com", "XXXXXX"); </remarks>
        /// </summary> 
        /// <param name="MailFrom">从哪个邮箱发送，也就是对方看见的从哪发来的</param>
        /// <param name="MailTo">发到哪个邮箱</param>
        /// <param name="Subject">邮件主题</param>
        /// <param name="Body">邮件内容</param>
        /// <param name="MailServer">发送方邮件服务器，如：stmp.163.com</param>
        /// <param name="MailUserName">登录smtp主机时用到的用户名,注意是邮件地址'@'以前的部分</param>
        /// <param name="MailUserPassword">发送方邮箱密码</param>
        /// <returns>0:OK; -1:失败</returns>
        public static int SendEmail(string MailFrom, string MailTo, string Subject, string Body, string MailServer, string MailUserName, string MailUserPassword)
        {
            MailAddress from = new MailAddress(MailFrom);
            MailAddress to = new MailAddress(MailTo);
            MailMessage message = new MailMessage(from, to);
            message.Subject = Subject;      //设置邮件主题 
            message.IsBodyHtml = true;      //设置邮件正文为html格式 
            message.Body = Body;            //设置邮件内容 

            SmtpClient client = new SmtpClient(MailServer);
            //设置发送邮件身份验证方式 
            //注意如果发件人地址是abc@def.com，则用户名是abc而不是abc@def.com 
            client.Credentials = new NetworkCredential(MailUserName, MailUserPassword);

            try
            {
                client.Send(message);
            }
            catch
            {
                return -1;
            }

            return 0;
        }

 
    }
}
